﻿#include "PhysicsDialogFEProperty.h"
#include "ui_MaterialDialogFE.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"

#include <QMessageBox>
#include <QComboBox>

namespace GUI
{
    PhysicsDialogFEProperty::PhysicsDialogFEProperty(PhysicsDlgExecType type, int sectionID, QWidget* parent)
        :AbstractMaterialDialog(type, parent)
    {
        _ui = new Ui::MaterialDialogFE;
        _ui->setupUi(this);

        _ui->tableWidget->clear();
        _ui->tableWidget->setColumnCount(0);
        _ui->tableWidget->setRowCount(0);
        _ui->label_image->setPixmap(QPixmap(":/browserIcons/bean_pic01.png"));
        setWindowTitle("FE Property Card");
        QStringList headerLabels;
        //表头
        headerLabels << tr(" Field 1 ") << tr(" PID ") << tr(" Field 3 ") << tr(" Field 4 ") << tr(" Field 5 ") 
             << tr(" Field 6 ") << tr(" Field 7 ") << tr(" Field 8 ") << tr(" Field 9 ") << tr(" Field 10 ");
        //设置表格列数和表头标签
        _ui->tableWidget->setColumnCount(headerLabels.size());
        _ui->tableWidget->setHorizontalHeaderLabels(headerLabels);
        //初始化表格行数
        _ui->tableWidget->setRowCount(1);
        QTableWidgetItem* item = new QTableWidgetItem(tr("PSHELL"));
        item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        _ui->tableWidget->setItem(0, 0, item);
        QTableWidgetItem* itemPID = new QTableWidgetItem();
        itemPID->setFlags(item->flags() & ~Qt::ItemIsEditable);
        _ui->tableWidget->setItem(0, 1, itemPID);

        Interface::FITKAbstractSectionManager *feMgr = getFEPropertyManager();

        //获取FE属性对象
        if (feMgr && sectionID > 0)
        {
            _feProperty = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(feMgr->getDataByID(sectionID));
            itemPID->setText(QString::number(sectionID));
        }
        this->init();
    }

    PhysicsDialogFEProperty::~PhysicsDialogFEProperty()
    {
        if (_ui) delete _ui;
    }

    void PhysicsDialogFEProperty::on_pushButton_Ok_clicked()
    {
        if (!this->checkInput()) return;
        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            _feProperty = new Interface::FITKAbaSectionShellHomogeneous;
            Interface::FITKAbstractSectionManager *feMgr = getFEPropertyManager();
            if (feMgr == nullptr) return;
            feMgr->appendDataObj(_feProperty);
        }
        if (!_feProperty)return;

        QString propertyName = _ui->lineEdit_Name->text();
        _feProperty->setDataObjectName(propertyName);
        QComboBox* cmb = this->findChild<QComboBox*>("MaterialBox");
        if (!cmb) return;
        int MID = cmb->currentData().toInt();
        _feProperty->setMaterial(MID);

        double thickness=_ui->tableWidget->item(0, 3)->text().toDouble();
        _feProperty->setThickness(thickness);

        this->accept();
    }

    void PhysicsDialogFEProperty::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void PhysicsDialogFEProperty::init()
    {
        Interface::FITKAbstractSectionManager *feMgr = getFEPropertyManager();
        if (feMgr == nullptr) return;
        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = feMgr->checkName("New FE Material");
            _ui->lineEdit_Name->setText(name);
        }
        // 初始化数据
        this->initData();

        //自适应宽度
        _ui->tableWidget->resizeColumnsToContents();
        // 设置每列的最小宽度为 100 像素
        for (int i = 0; i < _ui->tableWidget->columnCount(); ++i)
        {
            int currentWidth = _ui->tableWidget->columnWidth(i);
            if (currentWidth < 100) 
                _ui->tableWidget->setColumnWidth(i, 100);
            else if (currentWidth > 350)
                _ui->tableWidget->setColumnWidth(i, 350);
        }

        //复制,更新名称
        if (_feProperty && _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(_feProperty->getDataObjectName());
            _ui->lineEdit_Name->setText(text);
        }
    }

    void PhysicsDialogFEProperty::initData()
    {
        double thickness = 0;
        int MID=-1;
        if (_feProperty)
        {
            _ui->lineEdit_Name->setText(_feProperty->getDataObjectName());
            MID = _feProperty->getMaterialID();
            thickness = _feProperty->getThickness();
        }
        Interface::FITKMaterialManager *feMMgr = getFEMaterialManager();
        if (feMMgr == nullptr) return;

        //初始化FE材料下拉列表
        QComboBox* cmb = new QComboBox(this);
        cmb->setObjectName("MaterialBox");
        cmb->addItem("None", -1);
        for (int i = 0; i < feMMgr->getDataCount(); ++i)
        {
            Interface::FITKAbstractMaterial* data = feMMgr->getDataByIndex(i);
            if (!data)continue;
            cmb->addItem(data->getDataObjectName(), data->getDataObjectID());
        }
        int index = cmb->findData(MID);
        if (index >= 0)
            cmb->setCurrentIndex(index);
        _ui->tableWidget->setCellWidget(0, 2, cmb);

        _ui->tableWidget->setItem(0, 3, new QTableWidgetItem(QString::number(thickness)));

        //剩余field5 到 field10 不可编辑
        for (int i = 4; i < _ui->tableWidget->columnCount(); ++i)
        {
            QTableWidgetItem* item = new QTableWidgetItem("");
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
            _ui->tableWidget->setItem(0, i, item);
        }
    }

    bool PhysicsDialogFEProperty::checkInput()
    {
        //检查数据合法性
        if (!_ui->tableWidget->item(0, 3))return false;
        QString thinText = _ui->tableWidget->item(0, 3)->text();
        bool ok = false;
        thinText.toDouble(&ok);
        if (!ok)
        {
            QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
            return false;
        }
        QComboBox* cmb = this->findChild<QComboBox*>("MaterialBox");
        if (!cmb) return false;
        int ID = cmb->currentData().toInt();
        if (ID < 0)
        {
            QMessageBox::critical(nullptr, tr("Critical"), tr("Please select a material."));
            return false;
        }  
        Interface::FITKAbstractSectionManager *feMgr = getFEPropertyManager();
        if (feMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_Name->text().trimmed();
        if (text != "") {
            QString newText = feMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (feMgr->getDataByName(text) == _feProperty) return true;
        }
        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }

    Interface::FITKAbstractSectionManager * PhysicsDialogFEProperty::getFEPropertyManager()
    {
        //获取材料管理器
        Acoustics::FITKAcousticsDiscModel* disc = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (!disc)return nullptr;
        Acoustics::FITKAcousticsFEModel* feModel = disc->getFEModel();
        if (!feModel) return nullptr;
        return feModel->getSectionManager();
    }

    Interface::FITKMaterialManager * PhysicsDialogFEProperty::getFEMaterialManager()
    {
        //获取材料管理器
        Acoustics::FITKAcousticsDiscModel* disc = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (!disc)return nullptr;
        Acoustics::FITKAcousticsFEModel* feModel = disc->getFEModel();
        if (!feModel) return nullptr;
        return feModel->getMaterialManager();
    }

}
